//
//  CYContacts.m
//  CreditAddressBook
//
//  Created by Lee on 15/9/23.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYContacts.h"
#import "RHPerson.h"
#import "CNMutableContact+Extension.h"
#import "RHPerson+Extension.h"

@implementation CYContacts

+ (instancetype)contactsWithPerson:(id)person {
    CYContacts *con = [[CYContacts alloc] init];
    if ([person isKindOfClass:[RHPerson class]]) {
        con.person = person;
    } else {
#if IS_IOS9
        if ([person isKindOfClass:[CNContact class]]) {
            con.contact = person;
        }
#endif
    }
    return con;
}

+ (instancetype)autoContactsWithPerson:(id)person {
    CYContacts *con = [[CYContacts alloc] init];
#if IS_IOS9
    if ([person isKindOfClass:[RHPerson class]]) {
        con.contact = [self contactWithRHPerson:person];
    } else if ([person isKindOfClass:[CNContact class]]) {
        con.contact = person;
    }
#else
    if ([person isKindOfClass:[RHPerson class]]) {
        con.person = person;
    } else if ([person isKindOfClass:[CNContact class]]) {
        con.person = nil;
    }
#endif
    return con;
}

+ (CNMutableContact *)contactWithRHPerson:(RHPerson *)person {
    if (!person) return nil;
    CNMutableContact *contact = [[CNMutableContact alloc] init];
    NSArray *phones = person.formatterPhones;
    if (phones.count) {
        contact.phones = phones;
    }	
    if (person.note) {
        contact.note = person.note;
    }
    if (person.name) {
        contact.familyName = person.name;
    }
    if (person.originalImage) {
        contact.imageData = UIImageJPEGRepresentation(person.originalImage,1.0);
    } else if (person.thumbnail) {
        contact.imageData = UIImageJPEGRepresentation(person.thumbnail,1.0);
    }
    return contact;
}
@end
